import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class TradeMenu extends Menu{
	
	private BufferedImage offer_trade_menu;
	private boolean visible;
	private SOCApplet applet;
	private TradeOffer offer;
	private boolean offerIsSelected;
	
	public TradeMenu(SOCApplet applet){
		this.applet = applet;
		try {
			offer_trade_menu = ImageIO.read(new File("offer_trade_menu.gif"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		offerIsSelected = true;
		this.offer = new TradeOffer(applet.getTurn().getPlayer(), new int[5], new int[5]);
		MenuButton brickButton = new MenuButton("Brick Button", 89, 405, 45, 28, this);
		MenuButton grainButton = new MenuButton("Grain Button", 138, 405, 45, 28, this);
		MenuButton lumberButton = new MenuButton("Lumber Button", 188, 405, 45, 28, this);
		MenuButton oreButton = new MenuButton("Ore Button", 237, 405, 41, 28, this);
		MenuButton woolButton = new MenuButton("Wool Button", 279, 405, 42, 28, this);
		
		MenuButton exportButton = new MenuButton("Export Button", 90, 436, 229, 45, this);
		MenuButton importButton = new MenuButton("Import Button", 90, 515, 229, 45, this);
		MenuButton acceptButton = new MenuButton("Accept Button", 131, 484, 25, 25, this);
		MenuButton denyButton = new MenuButton("Deny Button", 248, 484, 25, 25, this);
		
		applet.addToClickable(brickButton);
		applet.addToClickable(grainButton);
		applet.addToClickable(lumberButton);
		applet.addToClickable(oreButton);
		applet.addToClickable(woolButton);
		applet.addToClickable(exportButton);
		applet.addToClickable(importButton);
		applet.addToClickable(acceptButton);
		applet.addToClickable(denyButton);
	}

	@Override
	public void activate(String buttonName) {
		if(buttonName.equals("Brick Button")){
			if (offerIsSelected)
				offer.addToOffer(0, 1);
			else
				offer.addToRequest(0, 1);
		} else if(buttonName.equals("Grain Button")){
			if (offerIsSelected)
				offer.addToOffer(1, 1);
			else
				offer.addToRequest(1, 1);
		} else if(buttonName.equals("Lumber Button")){
			if (offerIsSelected)
				offer.addToOffer(2, 1);
			else
				offer.addToRequest(2, 1);
		} else if(buttonName.equals("Ore Button")){
			if (offerIsSelected)
				offer.addToOffer(3, 1);
			else
				offer.addToRequest(3, 1);
		} else if(buttonName.equals("Wool Button")){
			if (offerIsSelected)
				offer.addToOffer(4, 1);
			else
				offer.addToRequest(4, 1);
		} else if(buttonName.equals("Export Button")){
			offerIsSelected = false;
		} else if(buttonName.equals("Import Button")){
			offerIsSelected = true;
		} else if(buttonName.equals("Accept Button")){
			if (offer.sendToBank()){
				visible = false;
			} else {
				
			}
		} else if(buttonName.equals("Deny Button")){
			visible = false;
		} else {
			visible = !visible;
		}
		
	}

	@Override
	public void drawSelf(Graphics g) {
		if(visible){
			g.drawImage(offer_trade_menu, 0, 380, null);
			g.setColor(Color.BLACK);
//			g.drawRect(90, 436, 229, 45);
//			g.drawRect(90, 515, 229, 45);
//			
//			g.drawRect(131, 484, 25, 25);
//			g.drawRect(248, 484, 25, 25);
		}
	}

}
